#upload files to server
import os
import traceback
from common.log import logger
import paramiko
from common import constants

host_ip = constants.KNOWLEDGE_EMBEDDINGS_DATA_SERVER_IP
host_port = 22
host_username = 'root'
host_password = 'Lingxird12345!@#'
REMOTE_PATH = constants.KNOWLEDGE_EMBEDDINGS_DATA_PATH

ROOT_PATH = os.path.dirname(os.path.realpath(__file__))

class FtpUtil():
    @classmethod
    def pushFile(cls, local_files):
        scp = paramiko.Transport((host_ip, host_port))
        scp.connect(username=host_username, password=host_password)
        sftp = paramiko.SFTPClient.from_transport(scp)
        try:
            # 将本地目录文件上传
            for file in local_files:
                remote_file = os.path.join(REMOTE_PATH, file.split('/')[-1])
                sftp.put(os.path.join(ROOT_PATH,file), remote_file)
                logger.info('上传文件 [{}] 成功'.format(file))
        except IOError:
            logger.info(traceback.format_exc())
        scp.close()

if __name__ == '__main__':
    # 服务器地址
    ftpUtil = FtpUtil()
    ftpUtil.pushFile(['raw_data/insurance_planner_gpt/规划师_用户疑义.csv',
                      'raw_data/insurance_planner_gpt/规划师_用户问题.csv',
                      'raw_data/insurance_planner_gpt/规划师_专业知识.csv',
                      'raw_data/insurance_planner_gpt/规划师_产品知识.csv',
                      'raw_data/insurance_planner_gpt/规划师_解决方案.csv'])

    # ftpUtil.pushFile(['embedding_base/insurance_planner_gpt/index.pkl',
    #                   'embedding_base/insurance_planner_gpt/index.faiss'])